using System;
using Gadgeteer.Modules.GHIElectronics;

namespace Coding4Fun.BluetoothPanoHead
{
    public enum JoyStickCommandState
    {
        None, 
        SingleClicked, 
        DoubleClicked
    }

    public enum JoyStickDirection
    {
        None,
        Left,
        Right,
        Up,
        Down
    }

    /// <summary>
    /// Maintains the current and previous state of the joystick.  Defines a double click state for the joystick
    /// </summary>
    public class JoyStickCommand
    {
        private JoyStickCommandState CurrentState { get; set; }
        private JoyStickDirection CurrentDirection { get; set; }
        private DateTime LastClick { get; set; }

        public JoyStickCommand()
        {
            ClearState();
            LastClick = DateTime.Now;
        }

        /// <summary>
        /// Manages the single click and double click states of the joystick
        /// </summary>
        /// <returns>The current click state of the joystick in JoyStickCommandState</returns>
        public JoyStickCommandState Click()
        {
            LastClick = DateTime.Now;
            if (CurrentState != JoyStickCommandState.DoubleClicked)
            {
                CurrentDirection = JoyStickDirection.None;
                CurrentState++;
            }
            return CurrentState;
        }

        /// <summary>
        /// Clears the joystick state.
        /// </summary>
        public void ClearState()
        {
            CurrentState = JoyStickCommandState.None;
            CurrentDirection = JoyStickDirection.None;
        }

        /// <summary>
        /// Sanity check to clear the click state after extended period of time
        /// </summary>
        /// <returns>The current click state of the joystick in JoyStickCommandState</returns>
        public JoyStickCommandState CheckState()
        {
            if ((DateTime.Now - LastClick).Seconds > 500)
            {
                ClearState();
            }
            return CurrentState;
        }

        /// <summary>
        /// Check the state of the joystick and return the current direction
        /// </summary>
        /// <returns>The current direction of the joystick in JoyStickDirection</returns>
        public JoyStickDirection CheckDirection()
        {
            CheckState();
            return CurrentDirection;
        }

        /// <summary>
        /// Converts the analog position to directional values for easier command parsing
        /// </summary>
        /// <param name="position">The physical position of the joystick.</param>
        /// <returns>The current direction of the joystick in JoyStickDirection</returns>
        public JoyStickDirection GetDirection(Joystick.Position position)
        {
            if (position.Y > 0.9)
            {
                CurrentDirection = JoyStickDirection.Up;
            }
            if (position.Y < 0.1)
            {
                CurrentDirection = JoyStickDirection.Down;
            }
            if (position.X > 0.9)
            {
                CurrentDirection = JoyStickDirection.Right;
            }
            if (position.X < 0.1)
            {
                CurrentDirection = JoyStickDirection.Left;
            }
            return CurrentDirection;
        }

    }

}
